package com.lovo.bbs.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.lovo.bbs.bo.UserInfoBo;
import com.lovo.bbs.vo.LoginVo;
import com.lovo.bbs.vo.UserInfoVo;

/**
 * 控制面板统一Servlet
 * 
 * @author tiancen2001
 */
public class ControlServlet extends HttpServlet implements Servlet {
	private static final long serialVersionUID = -3475326874054727542L;

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 页面类型(功能模块)
		String type = req.getParameter("type");
		// 会话用户
		LoginVo loginVo = (LoginVo) req.getSession().getAttribute("loginvo");
		String userName = loginVo.getUserName();
		String msg = null;// 返回信息

		UserInfoBo userInfoBo = new UserInfoBo();
		if ("account".equals(type)) {// 控制面板账户管理页
			String mail = req.getParameter("email");
			String formerpassword = req.getParameter("formerpassword");
			String password = req.getParameter("password");
			// 更新数据库
			UserInfoVo uservo = new UserInfoVo();
			uservo.setUserName(userName);
			uservo.setEmail(mail.trim());
			uservo.setPassword(password.trim());
			int changed = userInfoBo.chgMailPassword(uservo, formerpassword);
			if (changed == 0) {
				msg = "原密码不正确...";
			} else if (changed == 1) {
				msg = "修改成功...";
			}
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("account.jsp?username=" + userName)
					.forward(req, resp);
		} else if ("contact".equals(type)) {// 控制面板联系方式页
			String site = req.getParameter("site");
			String blog = req.getParameter("blog");
			String msn = req.getParameter("msn");
			String qq = req.getParameter("qq");
			String home = new String(req.getParameter("home").getBytes(
					"ISO-8859-1"), "utf-8");
			// 更新数据库
			UserInfoVo uservo = new UserInfoVo();
			uservo.setUserName(userName);
			uservo.setHomePage(site.trim());
			uservo.setBlog(blog.trim());
			uservo.setMsn(msn.trim());
			uservo.setQq(qq.trim());
			uservo.setHome(home.trim());
			int changed = userInfoBo.chgContactInfo(uservo);
			if (changed == 0) {
				msg = "修改失败,请稍候重试...";
			} else if (changed == 1) {
				msg = "修改成功...";
			}
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("contact.jsp?username=" + userName)
					.forward(req, resp);
		} else if ("special".equals(type)) {// 个性设置页
			UserInfoVo userInfo = userInfoBo.getUserInfo(userName,UserInfoBo.TYPE_OF_MEMBER);
			
			UserInfoVo vo = new UserInfoVo();
			int changed=0;
			//上传准备
			SmartUpload su = new SmartUpload();
			su.setMaxFileSize(1024*1024*1024);
			su.setTotalMaxFileSize(1024*1024*1024);
			su.setAllowedFilesList("jpg,bmp,gif,png,JPG,GIF,BMP,PNG");
			
			
			PageContext pageContext = JspFactory.getDefaultFactory()
					.getPageContext(this, req, resp, null, true, 8192, true);
			su.initialize(pageContext);
			
			try {
				//上传
				su.upload();
				
				//个性签名
				//签名依然是乱码,似乎是组件问题
				String sign = new String(su.getRequest().getParameter("sign").getBytes("ISO-8859-1"), "utf-8");
				String headpic = su.getRequest().getParameter("headpic");//选择的头像
				
				//如果用户原来的头像是上传的图像,则必须先删除原来的图像,以免图像文件夹无限制地膨胀
				String absAppPath = req.getSession().getServletContext().getRealPath("/");//项目绝对路径
				String absImgPath = absAppPath+"images\\userhead\\";//上传图像存储位置绝对路径的上一级目录
				if(userInfo.isCustomHeadImg()){
					File originalHeadImg = new File(absImgPath+userInfo.getHeadImg());//生成原头像文件对象
					//立即删除原头像
					if(originalHeadImg.exists()){
						originalHeadImg.delete();
					}
				}
				
				
				if(su.getFiles().getFile(0).getSize()==0){//没有上传图像时
					vo.setUserName(userName);
					vo.setSignature(sign);
					vo.setHeadImg(headpic);
					vo.setCustomHeadImg(false);
					//更新用户表(同时更新头像表)
					changed=userInfoBo.updateUserHead(vo,userInfo.getHeadImg());
				}else{//上传了图像时
					//生成无重复的文件名与扩展名
	                String filename=""+getTime()+"."+su.getFiles().getFile(0).getFileExt();
					//以生成的文件名另存
					su.getFiles().getFile(0).saveAs("/images/userhead/upload/"+filename,SmartUpload.SAVE_VIRTUAL); 
					//新头像
					String newHead = "upload\\"+filename;
					//重新设置头像
					vo.setUserName(userName);
					vo.setSignature(sign);
					vo.setHeadImg(newHead);
					vo.setCustomHeadImg(true);
					//更新用户表(同时更新头像表)
					changed=userInfoBo.updateUserHead(vo,userInfo.getHeadImg());
				}
			
			} catch (SmartUploadException e1) {
				e1.printStackTrace();
			}
			
			//返回信息
			if (changed == 0) {
				msg = "未能成功修改,请稍候重试...";
			} else if (changed == 1) {
				msg = "修改成功...";
			}
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("special.jsp?username=" + userName).forward(req, resp);
		}
	}	
	/**
	 * 返回时间值用作文件名
	 * @return
	 */
	public synchronized  long getTime(){
		Date date=new Date();
        long filename=date.getTime();
        return filename;
	}
}
